package com.huluobo.a2204kotlinshopping.detail

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2204kotlinshopping.R
import com.huluobo.a2204kotlinshopping.adapter.CommentAdapter
import com.huluobo.a2204kotlinshopping.databinding.FragmentCommentBinding
import com.huluobo.base.bean.CommentBean
import com.huluobo.base.view.BaseMVVMFragment

class CommentFragment : BaseMVVMFragment<FragmentCommentBinding, DetailViewModel>() {
    override fun getLayoutId(): Int = R.layout.fragment_comment

    private lateinit var commentAdapter: CommentAdapter
    private val commentList = mutableListOf<CommentBean>()

    override fun initData() {
        viewModel.getCommentList()

        viewModel.getCommentListSuccess.observe(this) {
            if (it.code == 200) {
                commentList.clear()
                commentList.addAll(it.data)
                commentAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getCommentListFailed.observe(this) {
            Log.i("TagA", "获取评论列表失败:$it")
        }
    }

    override fun initView() {
        dataBinding.commentRv.layoutManager = LinearLayoutManager(requireContext())
        commentAdapter = CommentAdapter(commentList)
        dataBinding.commentRv.adapter = commentAdapter
    }

    override fun getViewModel(): Class<DetailViewModel> {
        return DetailViewModel::class.java
    }
}